<?php
/**
 * Model类
 * 它的主要部分是对各种数据操作的函数
 */
class Model {
    var $db;
    function __construct($db) {
        $this->db=$db;
    }

    function categoryListNote() { //获取分类for index
        $re=$this->db->query("select * from category where uid=1 order by sort, id", MYSQL_ASSOC);
		$items = array();
		foreach ($re as $v){ //结构化二级分类数据
            $cnum = $v['cid'] - 1;
			if ($v['type']){ // 收入
                $item['income'][$cnum][] = $v;
			}else{
                $item['pay'][$cnum][] = $v;
            }
		}
        return $item;
    }
    function itemListNote() { //获取消费条目列表数据
        return $this->db->query("select money.*,category.caption as categorycaption from money left join category on category.id=money.category where money.status>0 order by money.id DESC", MYSQL_ASSOC);
    }
    function addNote() { // 获取添加页面的数据
        $id = getNum(fget('id'));
		$item_re = array();
        if ($id > 0){
            //获取要修改的条目
            $item_res = $this->db->query("select * from money where id={$id} limit 1", MYSQL_ASSOC);
			$item_re = $item_res[0];
			$option_selected[$item_re['type']] = $item_re['category']; // 选中的value
			$option_selected[$item_re['type'] ? 0 : 1] = $item_re['category']; // 选中的value
			$types = $item_re['type'];
        }else{
			$type = getNum(fget('type'), -1);
			$category = getNum(fget('category'), -1);
			if ($type > -1 && $category > -1){ // 从首页添加，含类型值
				$types = $type; // 消费类型
				$option_selected[$types] = $category; // 选中的value
				$option_selected[$types ? 0 : 1] = ''; // 选中的value
			}else{
				$types = 0; // 消费类型（默认为0）
				$option_selected[0] = $option_selected[1] = ''; // 选中的value
			}
		}
        //获取分类for添加页面
        $re=$this->db->query("select * from category order by sort", MYSQL_ASSOC);
		$option_values = array(); // 分类的value值
		$option_output = array(); // 分类的显示文字
		foreach ($re as $v){ //结构化
			$option_values[$v['type']][] = $v['id'];
            $option_output[$v['type']][] = $v['caption'];
		}
        return array($option_values, $option_output, $option_selected, $item_re, $types);
    }

    function postNote() { //插入一个新条目
        $id= getNum(fpost('item_id'), 0);
        $type=fpost('type');
		$category = $type ? fpost('category_income') : fpost('category_pay');
        $money=fpost('money');
        $pay_type=fpost('pay_type');
        $caption=fpost('caption');
        $content=fpost('content');
        $picture_type=fpost('pic_type');
        $picture=fpost('picture');
        $privacy=fpost('privacy');
		$year = fpost("Date_Year");
		$month = fpost("Date_Month");
		$day = fpost("Date_Day");
		$hour = fpost("Time_Hour");
		$minute = fpost("Time_Minute");
		$second = fpost("Time_Second");
		$generate_time = strtotime("{$year}-{$month}-{$day} {$hour}:{$minute}:{$second}");
		if ($id == 0){ //修改
			$add_time=time();
			$sql="INSERT INTO money (type, money, pay_type, caption, content, picture_type, picture, privacy, category, add_time, generate_time) VALUES({$type}, {$money}, {$pay_type}, '{$caption}', '{$content}', {$picture_type}, '{$picture}', {$privacy}, {$category}, {$add_time}, {$generate_time})";
		}else{ // 添加
			$sql = "update money set type={$type}, money={$money}, pay_type={$pay_type}, caption='{$caption}', content='{$content}', picture_type={$picture_type}, picture='{$picture}', privacy={$privacy}, category={$category}, generate_time={$generate_time}";
		}
        return $this->db->query($sql);
    }

    function deleteNote() {   //删除一个条目
        $sql="UPDATE money SET status=0 WHERE id=" . fget('id');
        return $this->db->query($sql);
    }

    function disableNote() {   //设置隐私
        $sql="UPDATE money SET privacy=1 WHERE id=" . fget('id');
        return $this->db->query($sql);
    }

    function enableNote() {   //设置公开
        $sql="UPDATE money SET privacy=0 WHERE id=" . fget('id');
        return $this->db->query($sql);
    }
}
?>